home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c.moderated,comp.std.c
- Subject: Re: Function returning structure
- Date: 24 Jan 1996 11:07:12 -0600
- Organization: Los Alamos National Laboratory
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4e5p00$6g9@solutions.solon.com>
- References: <4e2ki8$l0k@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- --text follows this line--
-
- Note cross-post to comp.std.c
-
- In article <4e2ki8$l0k@solutions.solon.com> bnelson@netcom.com (Bob
- Nelson) writes:
- <snip>
- /*
- Consider the following code -- in which a structure member is of type
- array of char and an instance of that structure is returned from a
- a function.
-
- The Standard [6.3.2.3] says that the dot operator designates a member
- of a structure. (It goes on to add -- in its example text -- that f().x
- is a valid postfix expression though not an lvalue, presuming that
- f is a function returning a structure and that x is member).
-
- With that in mind -- does the standard have anything that might
- disqualify member selection (in a non-assignment context) if that
- member is of type array and that structure is returned by a function?
-
- There is nothing wrong selecting that element. However, this element
- does not decay to a pointer to its first element. As a result, very
- few things can be done to it, except casting it to type void. In
- particular, one cannot index it with the [], assign it to a pointer
- variable, or pass it to printf or scanf.
-
- One current, popular compiler fails to compile this code -- diagnosing
- an "invalid use of non-lvalue array".
- */
- <snip>
- printf("%s\n", func().c);
-
- begin comp.std.c question {
- Is the following strictly conformant?
-
- #include <stdarg.h>
- #include <stdio.h>
- typedef struct a {char b[5];} a;
- typedef char c[5];
- int f(char *fmt, ...) { char *s;
- va_list ap; va_start(ap,x);
- s = va_arg(ap, c); va_end(ap);
- return printf(fmt,s); }
- int F(char *fmt, ...) { return 0; }
- a g(void) { static a y; return y; }
- int main(void) { f ( "%s", g().b ); return 0; }
-
- i.e. can non-lvalue arrays be passed to a function and accessed by
- the stdarg macros there? I do not think so, because the `return value'
- of va_arg is not an lvalue, and hence does not decay to a pointer if
- it has an array type.
-
- But what if one replaces f by F in main? i.e. can a non-lvalue array
- be passed to a vararg function at all? There seems to be no
- prohibition against that.
- }
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-